package edu.gatech.oad;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.SimpleAdapter;
import android.widget.Button;
import android.widget.ListView;


public class ItemList extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_item_list);

		/*
		 * put data shit in here.
		 */
		System.out.println("Started ItemList Activity");
		DataBaseHelper data = new DataBaseHelper(getApplicationContext());
		try {
			data.createDataBase();
			data.openDataBase();
		} catch (Exception E) {
			// do nothing
		}
		System.out.println("DB Created");
		ListView listView = (ListView) findViewById(R.id.mylist);

		if (!MainActivity.gblFiltered) {
			for (Item item : data.getItems()) {
				// change to get only found items
				MainActivity.gblItemList.add(item);
			}
		}
		// Define a new Adapter
		// First parameter - Context
		// Second parameter - Layout for the row
		// Third parameter - ID of the TextView to which the data is written
		// Forth - the Array of data
		//ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				//android.R.layout.simple_list_item_1, android.R.id.text1,
				//MainActivity.gblItemList);
		//SimpleAdapter adapter = new SimpleAdapter(this,MainActivity.gblItemList, android.R.layout.simple_list_item_2, android.R.id.text1);
		List<Map<String, String>> items = new ArrayList<Map<String, String>>();
		if(!MainActivity.gblItemList.isEmpty() || MainActivity.gblItemList != null)
		{
    		for(Item i :MainActivity.gblItemList){
    			Map<String, String> datum = new HashMap<String, String>(2);
    		    datum.put("title", i.itemDescription);
    		    datum.put("other", i.status + " on " + i.date + " in " + i.location);
    		    items.add(datum);
    		}
		}
		else
		{
			Map<String, String> datum = new HashMap<String, String>();
			datum.put("title", "No matches found");
			datum.put("other", "");
			items.add(datum);
		}
		SimpleAdapter adapter = new SimpleAdapter(this, items,
				android.R.layout.simple_list_item_2,
                new String[] {"title", "other"},
                new int[] {android.R.id.text1,
                           android.R.id.text2});
		
		// Assign adapter to ListView
		listView.setAdapter(adapter);
		MainActivity.gblFiltered = false;
		
		listView.setOnItemClickListener(new ListView.OnItemClickListener() {
			public void onItemClick(AdapterView<?> a, View v, int i, long l){
				Item current = MainActivity.gblItemList.get(i);
				ArrayList<String> itemData = new ArrayList<String>();
				for(String str : current.toString().split(";"))
				{
					itemData.add(str);
				}
				Intent b = new Intent(ItemList.this, ItemActivity.class);
				System.out.println(itemData.toString());
				b.putExtra("data", current.toString());
				startActivity(b);
			}			
		});

		Button filterButton = (Button) findViewById(R.id.filterButton);
		filterButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent b = new Intent(ItemList.this, FilterItemsActivity.class);
				startActivity(b);

			}
		});

		Button homeButton = (Button) findViewById(R.id.back);
		homeButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent b = new Intent(ItemList.this, MainActivity.class);
				startActivity(b);

			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.item_list, menu);
		return true;
	}

}
